package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.Func;
import com.company.project.dto.THwtjEntityDto;
import com.company.project.entity.SysDept;
import com.company.project.entity.SysUser;
import com.company.project.entity.THwtjEntity;
import com.company.project.entity.TSeatsEntity;
import com.company.project.mapper.SysDeptMapper;
import com.company.project.mapper.THwtjMapper;
import com.company.project.mapper.TSeatsMapper;
import com.company.project.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("tHwtjService")
public class THwtjServiceImpl extends ServiceImpl<THwtjMapper, THwtjEntity> implements THwtjService {

    @Resource
    private HttpSessionService httpSessionService;
    @Resource
    private UserService userService;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private THwtjMapper hwtjMapper;
    @Resource
    private DeptService deptService;
    @Resource
    private RoleService roleService;

    @Resource
    private TSeatsMapper tSeatsMapper;

    @Override
    public DataResult getPageList(Page page, THwtjEntity tHwtj) {
        try {
            List<THwtjEntityDto> hwtjEntityList = new ArrayList<>();
            IPage<THwtjEntityDto> iPage = null;
            //通过access_token拿userId
            String userId = httpSessionService.getCurrentUserId();
            SysUser sysUser = userService.getById(userId);
            String roleName = roleService.getRoleNames(userId).get(0);
            TSeatsEntity tSeatsEntity = tSeatsMapper.selectOne(Wrappers.<TSeatsEntity>lambdaQuery().eq(TSeatsEntity::getAccountid, userId));
            SysDept byId = deptService.getById(sysUser.getDeptId());
            tHwtj.setDeptNo(byId.getDeptNo());
            //4.3.1.超级管理员、代理商角色
            if (roleName.contains("管理")) {
                iPage = hwtjMapper.getListDL(page, tHwtj);
                return DataResult.success(iPage);
            } else if (roleName.contains("代理")) {
                iPage = hwtjMapper.getListDLS(page, tHwtj, sysUser);
                return DataResult.success(iPage);
            } else {
                //总经理角色、总监、主管
                List<SysDept> deptList = deptService.allDeptByDeptNo(byId.getDeptNo());
                int index = 0;
                for (SysDept dept : deptList) {
                    THwtjEntityDto hwtjByDept = hwtjMapper.getByDept(dept, tHwtj);

                    THwtjEntityDto hwtj = new THwtjEntityDto();
                    if (Func.isNotEmpty(hwtjByDept)) {
                        hwtj = hwtjByDept;
                    }
                    hwtj.setPid(dept.getPid());
                    hwtj.setId(dept.getId());
                    hwtj.setOrgname(dept.getName());
                    hwtjEntityList.add(hwtj);
                }
//                hwtjEntityList = hwtjMapper.getListJL(tHwtj);
                //给父节点名称赋值
                hwtjEntityList.parallelStream().forEach(entity -> {
                    SysDept parentDept = deptService.getById(entity.getPid());
                    if (parentDept != null) {
                        entity.setPidName(parentDept.getName());
                    }
                });
                //设置最大部门PID treeSpid 需求pid为0
                for (int i = 0; i < hwtjEntityList.size(); i++) {
                    if (hwtjEntityList.size() > 0 && StringUtils.equals(hwtjEntityList.get(i).getId(), hwtjEntityList.get(0).getId())) {
                        hwtjEntityList.get(i).setPid("0");
                    }
                }

                return DataResult.success(hwtjEntityList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("查询话务统计失败，请联系管理员！");
        }
    }

    @Override
    public DataResult infoData() {
        try {
            //通过access_token拿userId
            String userId = httpSessionService.getCurrentUserId();
            SysUser sysUser = userService.getById(userId);
            String username = sysUser.getUsername();
            String roleName = roleService.getRoleNames(userId).get(0);

            Map<String, List<String>> map = new HashMap<>();

            if (roleName.indexOf("管理员") > -1 || roleName.indexOf("代理") > -1) {
                //请选择公司、请选择销售、请选择代理、请选择线路;
                List<String> company = hwtjMapper.selectCompany(roleName, username);
                List<String> xs = hwtjMapper.selectXs(roleName, username);
                List<String> dl = hwtjMapper.selectDl(roleName, username);
                List<String> line = hwtjMapper.selectLine(roleName, username);
                map.put("company", company);
                map.put("xs", xs);
                map.put("dl", dl);
                map.put("line", line);
            }

            return DataResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("初始化选项失败，请联系管理员！");
        }
    }

    @Override
    public THwtjEntity getDayData(String roleName, SysUser sysUser) {
        THwtjEntity hwtj = new THwtjEntity();
        if (Func.equals("员工", roleName)) {
            hwtj = hwtjMapper.getDayDataRB(roleName, sysUser);
        } else {
            hwtj = hwtjMapper.getDayData(roleName, sysUser);
        }
        return hwtj;
    }
}